package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.system.dto.SysCompanyContactConfigDTO;
import com.nineclock.system.mapper.SysCompanyContactConfigMapper;
import com.nineclock.system.pojo.SysCompanyContactConfig;
import com.nineclock.system.service.SysCompanyContactConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SysCompanyContactConfigServiceImpl extends ServiceImpl<SysCompanyContactConfigMapper, SysCompanyContactConfig> implements SysCompanyContactConfigService {

    @Override
    public void add(SysCompanyContactConfigDTO dto) {

        if(dto==null|| StrUtil.isEmpty(dto.getName())){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //TODO 判断是否已存在数据

        SysCompanyContactConfig sccc = BeanHelper.copyProperties(dto, SysCompanyContactConfig.class);
        sccc.setCompanyId(CurrentUserHolder.get().getCompanyId());
        sccc.setFieldType("string");
        sccc.setStatus((short)1);
        this.save(sccc);

    }


    /**
     * 思路：
     * 1.查询公司ID下的所有通讯录配置
     * 2.如果查询为NULL，则初始化默认的通讯录配置关联到改公司下存入表中
     * 3.如果查询不为NULL，则直接返回
     * @return
     */
    @Override
    public List<SysCompanyContactConfig> findList() {

        List<SysCompanyContactConfig> dtoList = new ArrayList<>();

        long companyId = CurrentUserHolder.get().getCompanyId(); //默认用户ID
        LambdaQueryWrapper<SysCompanyContactConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysCompanyContactConfig::getCompanyId,companyId);

        List<SysCompanyContactConfig> scccList = this.list(lambdaQueryWrapper);

        //如果企业没有设置通讯录字段, 需要初始化默认值
        if(CollectionUtil.isEmpty(scccList)){
            scccList = new ArrayList<>();
            for (String name : NcConstant.COMPANY_CONFIG_CONTACT_LIST) {
                SysCompanyContactConfig config = new SysCompanyContactConfig();
                config.setCompanyId(companyId);
                config.setName(name);
                config.setType("fixed");
                config.setStatus((short)1);
                config.setFieldType("string");

                this.save(config);

                scccList.add(config);
            }
        }

        return scccList;
    }


    /**
     * 思路：
     * 1.根据ID查询数据
     * 2.如果不存在则抛异常
     * 3.如果存在则更新
     * @param id
     * @param status
     */
    @Override
    public void update(Long id, Short status) {
        //判断是否存在
        SysCompanyContactConfig sysCompanyContactConfig = this.getById(id);
        if(sysCompanyContactConfig==null){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }

        sysCompanyContactConfig.setStatus(status);
        sysCompanyContactConfig.setCompanyId(CurrentUserHolder.get().getCompanyId());
        sysCompanyContactConfig.setId(id);
        this.updateById(sysCompanyContactConfig);
    }


    @Override
    public void delete(Long id) {
        //TODO 判断是否存在
        //TODO 判断是否允许删除
        SysCompanyContactConfig sysCompanyContactConfig = this.getById(id);
        if(sysCompanyContactConfig==null){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }
        this.removeById(id);
    }

}
